//
//  YKFarmDataListController.m
//  YK_BaseMediator
//
//  Created by edward on 2021/3/11.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKFarmDataListController.h"
#import "YKFarmDetailController.h"
#import "YKFarmViewModel.h"
#import "FarmDataListCell.h"
#import <YK_TableRefresh/YK_TableRefresh.h>
#import <YKAlert/YKAlertCenter.h>
#import <Masonry/Masonry.h>
#import "YKFarmDetailAddController.h"
#import <YK_ExectionModel/YKNSObjectCore.h>
#import <YK_ExectionModel/YKUIKitCore.h>

@interface YKFarmDataListController ()<UITableViewDelegate,UITableViewDataSource>
///
@property(nonatomic,copy) NSString *farmDataId;
///
@property(nonatomic,strong) YKFarmViewModel *viewModel;
///
@property(nonatomic,strong) UITableView *tableView;

@end

@implementation YKFarmDataListController

- (instancetype)initWithDataId:(NSString *)farmDataId viewModel:(YKFarmViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.farmDataId = farmDataId;
        self.viewModel = viewModel;
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [btn setTitle:@"添加记录" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn sizeToFit];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = item;
    
    @weakify(self);
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        YKFarmDetailAddController *vc = [[YKFarmDetailAddController alloc] initWithBlock:^(NSString * _Nonnull time) {
            [self.viewModel.addfarmDataListCommand execute:@{@"farmlistId":self.farmDataId,@"date":time}];
        }];
        [self presentViewController:vc animated:YES completion:nil];
    }];
    
    [self.view addSubview:self.tableView];
    [self.viewModel.farmDataListCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    [[self.viewModel.farmDataListCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            [YKAlertCenter showLoading:@"正在加载"];
        }else
        {
            [YKAlertCenter dissLoading];
        }
    }];
    [self.viewModel.deletefarmDataListCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [YKAlertCenter showMessage:@"删除成功"];
        [self.viewModel.farmDataListCommand execute:self.farmDataId];
    }];
    [self.viewModel.addfarmDataListCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [YKAlertCenter showMessage:@"创建成功"];
        [self.viewModel.farmDataListCommand execute:self.farmDataId];
    }];
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.viewModel.farmDataListCommand execute:self.farmDataId];
    }];
    [self.viewModel.farmDataListCommand execute:self.farmDataId];
}
#pragma mark -tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.farmDataListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FarmDataListCell *cell = [tableView dequeueReusableCellWithIdentifier:FarmDataListCell.defaultIdentifier];
    [cell configDataWithViewModel:self.viewModel indexPath:indexPath dataSource:self.viewModel.farmDataListArray];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weakify(self);
    [self.viewModel getFarmDataListDetail:indexPath block:^(NSString * _Nonnull date, NSString * _Nonnull sumDrug, NSString * _Nonnull sumFood, NSString * _Nonnull farmId) {
        @strongify(self);
        YKFarmDetailController *vc = [[YKFarmDetailController alloc] initWithViewModel:self.viewModel farmId:farmId];
        vc.title = date;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    @weakify(self);
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        /// todo
        @strongify(self);
        [self.viewModel getFarmDataListDetail:indexPath block:^(NSString * _Nonnull date, NSString * _Nonnull sumDrug, NSString * _Nonnull sumFood, NSString * _Nonnull farmId) {
            [self.viewModel.deletefarmDataListCommand execute:farmId];
        }];
    }];
    return @[deleteRowAction];
}
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 85;
        tableView.rowHeight = 100;
        tableView.backgroundColor = UIColor.clearColor;
        @weakify(self);
        tableView.mj_header = [YKRefreshHeader headerNormalWithViewModel:self.viewModel dataSource:self.viewModel.farmListArray refreshingBlock:^{
            @strongify(self);
            //刷新动作
            [self.viewModel.farmDataListCommand execute:self.farmDataId];
        }];
        tableView.mj_footer = [YKRefreshFooter footerWithViewModel:self.viewModel refreshingBlock:^{
            @strongify(self);
            [self.viewModel.farmDataListCommand execute:self.farmDataId];
        }];
        [tableView registerClass:FarmDataListCell.class forCellReuseIdentifier:FarmDataListCell.defaultIdentifier];
        _tableView = tableView;
    }
    return _tableView;
}

- (void)dealloc
{
    [_viewModel clearDataList];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
